#include "ConfigureLeds.h"
#include "ui_ConfigureLeds.h"
#include "ScreenPresentation.h"
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QResizeEvent>
#include <math.h>

ConfigureLeds::ConfigureLeds(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ConfigureLeds)
{
	ui->setupUi(this);

	ui->widget->setSelectable(true);

    ui->ledDirection->addItem("Clockwise");
    ui->ledDirection->addItem("Counter-clockwise");

	//ui->screenRatio->addItem("5:3",QVariant(5/3));
    ui->screenRatio->addItem("4:3",QVariant(4/3));
	//ui->screenRatio->addItem("1.6:1",QVariant(1.6/1));
    ui->screenRatio->addItem("16:9",QVariant(16/9));
	//ui->screenRatio->addItem("2.4:1",QVariant(2.4/1));
	ui->screenRatio->setCurrentIndex(1);

    ui->screenType->addItem("center leg");
    ui->screenType->addItem("left leg");
    ui->screenType->addItem("no leg");

    ui->stripeType->addItem("15 cm");
    ui->stripeType->addItem("10 cm");

    for(int i=15;i<30;i++) ui->screenSize->addItem(QString("%1''").arg(i));

    ui->radioAuto->setChecked(true);

	//_thread = new TestLeds(this);

	connect(ui->spinBoxHorizontal,SIGNAL(valueChanged(int)),SLOT(onHorChanged(int)));
	connect(ui->spinBoxVertical,SIGNAL(valueChanged(int)),SLOT(onVerChanged(int)));
	connect(ui->btnTest,SIGNAL(clicked()),SLOT(onBtnTest()));
    connect(ui->ledDirection,SIGNAL(currentIndexChanged(int)),SLOT(onDirectionChanged(int)));
    connect(ui->screenCount,SIGNAL(currentIndexChanged(int)),SLOT(onUpdateView()));
    connect(ui->screenRatio,SIGNAL(currentIndexChanged(int)),SLOT(onUpdateView()));
    connect(ui->screenSize,SIGNAL(currentIndexChanged(int)),SLOT(onUpdateView()));
    connect(ui->screenType,SIGNAL(currentIndexChanged(int)),SLOT(onUpdateView()));
    connect(ui->stripeType,SIGNAL(currentIndexChanged(int)),SLOT(onUpdateView()));
    connect(ui->radioAuto,SIGNAL(toggled(bool)),SLOT(onRadioBtn()));
    connect(ui->radioManual,SIGNAL(toggled(bool)),SLOT(onRadioBtn()));

    _imageLabel = new QLabel(ui->widget);
	QPixmap p = QPixmap(":/images/clockwise.png");
    _imageLabel->setPixmap(p.scaled(20,20,Qt::KeepAspectRatio,Qt::SmoothTransformation));
    _imageLabel->setGeometry(10,10,20,20);

    QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(this);
    effect->setOpacity(0);
    _imageLabel->setGraphicsEffect(effect);

	onHorChanged(0);
}

int ConfigureLeds::alpha()
{
    return 0;
}

void ConfigureLeds::setAlpha(int a)
{
    QGraphicsOpacityEffect* effect = (QGraphicsOpacityEffect*) _imageLabel->graphicsEffect();
    effect->setOpacity((double)a/100);
}

ConfigureLeds::~ConfigureLeds()
{
	delete ui;
}

void ConfigureLeds::onHorChanged(int num)
{
	onRadioBtn();
	//ui->widget->setLedsCount(num,ui->spinBoxVertical->text().toInt());
	ui->widget->update1();
}

void ConfigureLeds::onVerChanged(int num)
{
	onRadioBtn();
	//ui->widget->setLedsCount(ui->spinBoxHorizontal->text().toInt(),num);
	ui->widget->update1();
}

void ConfigureLeds::onBtnTest()
{
	TestLeds* _thread = new TestLeds(this);
	connect(_thread,SIGNAL(finished()),_thread,SLOT(deleteLater()));
	_thread->setLeds(ui->widget->getLeds(), ui->widget);
	_thread->start();
}

void ConfigureLeds::onDirectionChanged(int index)
{
    QRectF rect = ui->widget->getScreenRect();
    int w = rect.width();
    int h = rect.height();
    int sz = w<h ? w/2 : h/2;
    _imageLabel->setGeometry(ui->widget->width()/2-sz/2,ui->widget->height()/2-sz/4*3,sz,sz);
	QPixmap p = QPixmap(!index ? ":/images/clockwise.png" : ":/images/conterclockwise.png");
    _imageLabel->setPixmap(p.scaled(sz,sz,Qt::KeepAspectRatio,Qt::SmoothTransformation));

    ui->widget->setDirection(index == 0);

    QPropertyAnimation *animation = new QPropertyAnimation(this, "alpha");
    animation->setDuration(2000);
    animation->setKeyValueAt(0, 0);
    animation->setKeyValueAt(0.5, 255);
    animation->setKeyValueAt(1, 0);
    animation->setLoopCount(1);
    animation->start();
}

void ConfigureLeds::onUpdateView()
{
    ui->widget->setScreenNumber(ui->screenCount->currentText().toInt());
	onRadioBtn();

	if(ui->screenRatio->currentIndex() == 1) ui->widget->setMode(ScreenMode::Middle_16_9);
	else ui->widget->setMode(ScreenMode::Middle_4_3);

    QApplication::sendEvent(ui->widget,new QResizeEvent(QSize(),QSize()));
    ui->widget->update1();
}

void ConfigureLeds::onRadioBtn()
{
    if(ui->radioAuto->isChecked()){
		int w = 16;
		int h = 9;
		int sum = 2*(ui->screenSize->currentIndex()+15)*(w+h)/sqrt(w*w+h*h)*0.0254*(15+ui->stripeType->currentIndex()*15)*(1+ui->screenCount->currentIndex());
		ui->widget->setLedsCount(sum);
		//ui->widget->setLedsCount(ui->spinBoxVertical->text().toInt()*2 + ui->spinBoxHorizontal->text().toInt()*2);
    }
    else{
        ui->widget->setLedsCount(ui->spinBoxVertical->text().toInt(), ui->spinBoxHorizontal->text().toInt());
    }
}

///////////////////////////////////////////////////////////////////

TestLeds::TestLeds(QObject* parent) :
	QThread(parent),
	_isRunning(true),
	_widget(NULL)
{
}

void TestLeds::stop()
{
	_isRunning = false;
}

void TestLeds::setLeds(QList<LedView*> list, ScreenPresentation* widget)
{
	_leds = list;
	_widget = widget;
}

void TestLeds::run()
{
    if(_leds.count() == 0) return;
	LedView* save = NULL;
	QColor saveCol;
	bool start = false;
	int num = -1;
	while(1){
		num++;
		LedView* led = _leds.at(num%_leds.count());
		if(!_isRunning) break;
		if(start && led->isLedStart()) break;
		if(!led->isLedActive()) continue;
		if(led->isLedStart()) start = true;
		if(!start) continue;

        if(save) save->setColor(saveCol);
        save = led;
        saveCol = led->getColor();
        led->setColor(QColor(0,170,0));
		if(_widget) _widget->update1();
		msleep(150);
	}
	if(save) save->setColor(saveCol);
    if(_widget) _widget->update1();
}
